package hot;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class fourtynine {
    public static List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String,List<String>> hashMap=new HashMap<>();
        for(String x:strs){
            //将字符串转换为字符数组
            char[] tmp=x.toCharArray();
            Arrays.sort(tmp);
            //再将字符数组转换为字符串
            String key=new String(tmp);
            if(!hashMap.containsKey(key)){
                hashMap.put(key,new ArrayList<>());
            }
            hashMap.get(key).add(x);
        }
        return new ArrayList(hashMap.values());

    }

    public static void main(String[] args) {
//        输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
//
//        输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
//
//        解释：
        String[] strings={"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> listList = groupAnagrams(strings);
        System.out.println(listList);

    }

}
